// components/phoneBottom/phoneBottom.js
var gb = getApp().globalData
var server =gb.server
var cfg = gb.cfg
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的初始数据
   */
  data: {
    UnNeedPostPhone:true
  },
  attached:function(){
    console.log('组件 phoneBottom 初始化完成')

    var app = getApp()
    app.subscribe('UnNeedPostPhone', (UnNeedPostPhone) => {
      this.setData({
        UnNeedPostPhone,
      });
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getPhoneNumber(e) {
      console.log('获取手机号返回结果',e)
      var self =this
      if (e.detail.errMsg !=="getPhoneNumber:ok"){
          return;
      }
      var app = getApp()
      wx.checkSession({
        success (){
          //session_key 未过期，并且在本生命周期一直有效
          cfg.show_load()
          server.post('/dapi/wxmin/phone', {
            info:  e.detail //JSON.stringify(e.detail)
          }).then((resp) => {
            cfg.hide_load()
            if(resp.data.phone){
              app.globalData.UnNeedPostPhone = true
              app.globalData.userInfo.phone = resp.data.phone
              self.triggerEvent('hasphoneEvent')
            }else if(resp.data.operation=='need_relogin'){
              wx.showModal({
                title: '提示',
                content:'手机号码获取失败，请重新授权登录后重试',
                success(resp){
                  if(resp.confirm){
                    app.login() //重新登录
                  }
    
                }
              })
            }
            
          })
        },
        fail () {
          // session_key 已经失效，需要重新执行登录流程
   
         
        }
      })

     
     
    },
    onTap: function(){
      this.triggerEvent('hasphoneEvent')
    }
  }
})
